package com.suixinsanren.design.state.demo.demo01;

public class GumballMachine {

    private State soldState;
    private State hasQuarterState;
    private State noQuarterState;
    private State soldOutState;
    private State winterState;

    private State state;
    private int count = 0;

    public GumballMachine(int numberGumballs) {
        this.soldState = new SoldState(this);
        this.hasQuarterState = new HasQuarterState(this);
        this.noQuarterState = new NoQuarterState(this);
        this.soldOutState = new SoldOutState(this);
        this.winterState = new WinterState(this);
        if (numberGumballs > 0) {
            this.count = numberGumballs;
            state = noQuarterState;
        } else {
            state = soldOutState;
        }
    }

    public void insertQuarter() {
        state.insertQuarter();
    }

    public void ejectQuarter() {
        state.ejectQuarter();
    }

    public void turnCrank() {
        state.turnCrank();
        state.dispense();
    }

    public void releaseBall() {
        System.out.println("一颗糖果从狭缝里滚了出来");
        if (count != 0) {
            count--;
        }
    }

    public void refill(int numberGumballs) {
        if (numberGumballs > 0) {
            System.out.println("补充了" + numberGumballs + "个糖果");
            count += numberGumballs;
            state = noQuarterState;
        }
    }

    @Override
    public String toString() {
        return "GumballMachine{" +
                "state=" + state +
                ", count=" + count +
                '}';
    }

    public State getSoldState() {
        return soldState;
    }

    public void setSoldState(State soldState) {
        this.soldState = soldState;
    }

    public State getHasQuarterState() {
        return hasQuarterState;
    }

    public void setHasQuarterState(State hasQuarterState) {
        this.hasQuarterState = hasQuarterState;
    }

    public State getNoQuarterState() {
        return noQuarterState;
    }

    public void setNoQuarterState(State noQuarterState) {
        this.noQuarterState = noQuarterState;
    }

    public State getSoldOutState() {
        return soldOutState;
    }

    public void setSoldOutState(State soldOutState) {
        this.soldOutState = soldOutState;
    }

    public State getState() {
        return state;
    }

    public void setState(State state) {
        this.state = state;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public State getWinterState() {
        return winterState;
    }

    public void setWinterState(State winterState) {
        this.winterState = winterState;
    }
}
